<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function render(template, data) {
        const reg = /\{\{(\w+)\}\}/; // 模板字符串正则

        if (reg.test(template)) {
          let name = reg.exec(template)[1];
          template = template.replace(reg, data[name]);
          return render(template, data); // 递归渲染
        }
        return template;
      }

      let template = "我是{{name}},今年{{age}}岁了!";
      console.log(render(template, { name: "张三", age: 24 }));
    </script>
  </body>
</html>
